package nc.ui.wa.CommissionSum.model;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.itf.wa.CommissionSum.ICommissionSumQueryService;
import nc.ui.hr.uif2.model.IHrAppModelDataManagerEx;
import nc.ui.uif2.components.pagination.BillManagePaginationDelegator;
import nc.ui.uif2.components.pagination.IPaginationModelListener;
import nc.ui.uif2.components.pagination.PaginationBar;
import nc.ui.uif2.components.pagination.PaginationModel;
import nc.ui.uif2.model.DefaultAppModelDataManager;
import nc.ui.uif2.model.IAppModelDataManagerEx;
import nc.vo.hi.pub.HICommonValue;
import nc.vo.pub.BusinessException;
import nc.vo.pub.BusinessRuntimeException;
import nc.vo.wa.commissionsum.AggCommSumVO;

import org.apache.commons.lang.StringUtils;

@SuppressWarnings("restriction")
public class CommissionSumModelDataManager extends DefaultAppModelDataManager
		implements IAppModelDataManagerEx, IHrAppModelDataManagerEx,
		IPaginationModelListener {

	private String sqlByWhere;
	private BillManagePaginationDelegator paginationDelegator;
	private PaginationModel paginationModel;
	private PaginationBar paginationBar = null;
	private CommissionSumModelDataManager queryService;

	@Override
	public void initModel() {
		super.initModel();
		refresh();
	}

	@Override
	public CommissionSumAppModel getModel() {
		return (CommissionSumAppModel) super.getModel();
	}

	@Override
	public void initModelBySqlWhere(String paramStr) {
		sqlByWhere = paramStr;
		refresh();
	}

	@Override
	public void refresh() {
		if (StringUtils.isEmpty(getContext().getPk_org())) {
			getModel().initModel(null);
			return;
		}
		StringBuffer condition = new StringBuffer(" 1=1 ");
		if (sqlByWhere != null) {
			condition.append(" and " + sqlByWhere);
		}

		AggCommSumVO[] aggvos = null;
		try {
			aggvos = NCLocator
					.getInstance()
					.lookup(ICommissionSumQueryService.class)
					.queryByCondition(getContext(), condition.toString(),
							"apply_time desc");
		} catch (BusinessException e) {
			Logger.error(e.getMessage());
			throw new BusinessRuntimeException(e.getMessage(), e);
		}
		getModel().initModel(aggvos);
	}

	@Override
	public void setShowUnUseDataFlag(boolean arg0) {

	}

	@Override
	public void setShowSealDataFlag(boolean arg0) {
	}

	@Override
	public void onStructChanged() {
	}

	@Override
	public void onDataReady() {
		getPaginationDelegator().onDataReady();
	}

	public BillManagePaginationDelegator getPaginationDelegator() {
		if (paginationDelegator == null) {
			paginationDelegator = new BillManagePaginationDelegator(getModel(),
					getPaginationModel());
		}
		return paginationDelegator;
	}

	public void setPaginationDelegator(
			BillManagePaginationDelegator paginationDelegator) {
		this.paginationDelegator = paginationDelegator;
	}

	public PaginationModel getPaginationModel() {
		return paginationModel;
	}

	public void setPaginationModel(PaginationModel paginationModel)
			throws BusinessException {
		this.paginationModel = paginationModel;
		paginationModel.addPaginationModelListener(this);
		paginationModel.setMaxPageSize(HICommonValue.MAX_ROW_PER_PAGE);
		paginationModel.setPageSize(2);
		paginationModel
				.setPaginationQueryService((nc.ui.uif2.components.pagination.IPaginationQueryService) queryService);
		// this.paginationModel = paginationModel;
		paginationModel.init();
	}

	public PaginationBar getPaginationBar() {
		return paginationBar;
	}

	public void setPaginationBar(PaginationBar paginationBar) {
		this.paginationBar = paginationBar;
	}

	public CommissionSumModelDataManager getQueryService() {
		return queryService;
	}

	public void setQueryService(CommissionSumModelDataManager queryService) {
		this.queryService = queryService;
	}

}
